﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using Payton.WP7.Common.Interface;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.ComponentModel;

namespace Payton.WP7.Common
{
    public class CompositeStatusService : StatusServiceBase
    {
        IDictionary<Model.SNSAccount, IStatusService> _svcs = new Dictionary<Model.SNSAccount, IStatusService>();

        public void Register(Model.SNSAccount account, IStatusService svc)
        {
            if (!_svcs.ContainsKey(account))
            {
                _svcs.Add(account, svc);
            }
        }

        public void Remove(Model.SNSAccount account)
        {
            if (_svcs.ContainsKey(account))
            {
                _svcs.Remove(account);
            }
        }

        public bool Contains(Model.SNSAccount account)
        {
            return _svcs.ContainsKey(account);
        }

        #region StatusServiceBase Override


        #endregion

        public override void UpdateStatus(Model.SNSRequest request, Action<Model.SNSStatus, SNSResult> action)
        {
            if (!_svcs.Keys.Any(a => a.IsDestination))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = "请至少选择一个账号分享" });
                }
                return;
            }

            foreach (var svc in _svcs)
            {
                if (svc.Key.IsDestination && !svc.Key.IsDuplicate)
                {
                    ThreadPool.QueueUserWorkItem((service) =>
                        {
                            var s = service as IStatusService;
                            if (s != null)
                                s.UpdateStatus(request, action);
                        }, svc.Value);
                }
            }

        }

        public override void GetTimeline(Model.SNSRequest request, Action<IList<Model.SNSStatus>, SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = true, Message = "没有绑定账号, 或者没有启用任何账号" });
                }
            }
            ThreadPool.QueueUserWorkItem((o) =>
            {
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate)
                        svc.Value.GetTimeline(request, action);
                    Thread.Sleep(100);
                }
            });
        }

        protected override string[] GetParamtersList(Model.SNSType type)
        {
            throw new NotImplementedException();
        }

        protected override int ValidateResponse(Hammock.RestResponse o, out string msg)
        {
            msg = "成功";

            return 1;
        }

        protected override IList<Model.SNSStatus> Mapping(string res, SNSResult result)
        {
            throw new NotImplementedException();
        }

        protected override void Initialize()
        {
            //throw new NotImplementedException();
        }
    }
}
